package com.sin.j2me.rms;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class RMSNVObject {
	String name = null;
	byte[] value = null;
	
	public RMSNVObject() {
		super();
	}
	public RMSNVObject(String name, byte[] value) {
		super();
		this.name = name;
		this.value = value;
	}
	public RMSNVObject(byte[] bytes) throws IOException {
		super();
		fromByte(bytes);
	}
	public byte[] toBytes() throws IOException{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		dos.writeUTF(name);
		dos.write(value);
		byte[] res = baos.toByteArray();
		dos.close();
		baos.close();
		return res;
	}
	public void fromByte(byte[] bytes) throws IOException{
		ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 
		DataInputStream dis = new DataInputStream(bais);
		this.name = dis.readUTF();
		byte[] buf = new byte[1024];
		int len = dis.read(buf);
		this.value = new byte[len];
		System.arraycopy(buf, 0, this.value, 0, len);
		dis.close();
		bais.close();
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public byte[] getValue() {
		return value;
	}
	public void setValue(byte[] value) {
		this.value = value;
	}
	public String toString() {
		return "RmsNVPair [name=" + name + ", value len:" + value.length + "]";
	}
}
